// 验证是否登录过
if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
               var url=location.href;
               var tiao=true;
             $.each(res.data[0].power_list,function (i,o) {
                 console.log(o);
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                     var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

               $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');
             });

             if(tiao){
                 location.href=res.data[0].power_list[0].url;
             }

            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        }
    });
} else {
    location.href = key_address;
}

$(function () {
    // 后台对象
    var houtai = {
        linshi_pid: "",  //临时一级id
        linshi_cid: "",  //临时二级id
        linshi_catid: "",//临时三级id
        linshi_search:{
            class1:"",
            class2:"",
            search:""
        },
        queren_type: "",//1 二级修改 2 二级添加 3 二级删除 4三级添加 5 三级修改 6三级删除 7上线2级分类 8 下线2级分类 9 上线3级分类 10 下线三级分类
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        //初始分类
        chushi: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/classify/product",
                type: "post",
                async:false,
                data:{
                    token:get_cookie("key")
                },
                success: function (res) {
                    //写入操作分类table
                    console.log(JSON.parse(res));

                    if (JSON.parse(res).resStatus === 0) {
                        houtai.arr = JSON.parse(res).data;
                        if (!sessionStorage.one) {
                            sessionStorage.one = houtai.arr[0].pid;
                        }
                        if (!sessionStorage.two) {
                            sessionStorage.two = houtai.arr[0].activity_list[0].cid;
                        }

                        for (var ii = 0, string11 = "", string22 = "", string33 = ""; ii < houtai.arr.length; ii++) {
                            string11 += '<option value="' + houtai.arr[ii].pid + '">' + houtai.arr[ii].typesnames + '</option>';
                            if (houtai.arr[ii].pid == sessionStorage.one) {
                                for (var aa = 0; aa < houtai.arr[ii].activity_list.length; aa++) {
                                    string22 += '<option value="' + houtai.arr[ii].activity_list[aa].cid + '">' + houtai.arr[ii].activity_list[aa].classnames + '</option>';
                                    if(houtai.arr[ii].activity_list[aa].cid == sessionStorage.two){
                                        for(var bb=0;bb<houtai.arr[ii].activity_list[aa].cats_list.length;bb++){
                                            string33 += '<option value="' +houtai.arr[ii].activity_list[aa].cats_list[bb].catId+ '">' + houtai.arr[ii].activity_list[aa].cats_list[bb].catName+ '</option>';
                                        }
                                        $("#select3").empty().append(string33);
                                    }
                                }
                                $("#select2").empty().append(string22);
                            }
                        }
                        $("#select1").empty().append(string11);
                        $("#select1").val(sessionStorage.one);
                        $("#select2").val(sessionStorage.two);
                    }
                },
                error:function () {
                    houtai.hint("初始分类请求失败！！");
                }

            });
        },
        shop_list_ajax: function (page_number) {
            var n = page_number ? page_number : houtai.list_ajax_page=1;
            $.ajax({
                url: ip + "index.php?s=admincms/classify/getCatsFreightList",
                type: "post",
                data: {
                    token: get_cookie("key"),
                    cat1:houtai.linshi_search.class1,
                    cat2:houtai.linshi_search.class2,
                    search:houtai.linshi_search.search
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    houtai.table_string(res);
                    $("#page").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount:res.totalpage,           //总条数
                        pageSize:10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.list_ajax_page=pageIndex;
                            houtai.shop_list_ajax(pageIndex);
                        }
                    });

                }
            });
        },
        table_string: function (res) {
            // 写入table
            var string_head = "<thead><tr><th>ID</th><th>分类名称</th><th>楼梯价格/层</th><th>电梯价格</th><th>操作</th></tr></thead>";
            var string_body = "";
            if (res.resStatus===0) {
                $.each(res.data, function (i, o) {
                    string_body +='<tr>' +
                        '<td style="vertical-align: middle" class="up_building_id">' +o.catId + '</td>' +
                        '<td style="vertical-align: middle" class="up_building_name">' + o.catName + '</td>' +
                        '<td style="vertical-align: middle" class="up_building_louti">' + o.stairsPrice + '</td>' +
                        '<td style="vertical-align: middle" class="up_building_dianti">' +o.elevatorPrice + '</td>' +
                        '<td style="vertical-align: middle;width: 150px;" class="gongcheng_controls text-center"> <button type="button" class="btn btn-warning btn-xs set"> <span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 修改</button> <button type="button" class="btn btn-danger btn-xs remove"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除</button></td>' +
                        '</tr>';
                });
            }else{
                string_body = "<tr class='text-center'><td colspan='13'>没有数据</td></tr>";
            }

            $("#up_building_price").empty().append(string_head + string_body);
        },
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        jiancha: function () {
            // 判断是够为空
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint("请填写 " + $(".add_goods_body .input_is:visible").eq(i).prev().text());
                    return false;
                }
            }
            return true;
        },
        add:function () {
            $.ajax({
                url: ip + "index.php?s=admincms/classify/addCatsPrice",
                type: "post",
                data: {
                    catId:houtai.linshi_catid,
                    stairsPrice:$.trim($("#louti_price_add").val()),
                    elevatorPrice:$.trim($("#dianti_price_add").val()),
                    token:get_cookie("key")
                },
                success: function (res) {
                    if (JSON.parse(res).resStatus === 0) {
                        houtai.hint("<span style='color:#0f0'>上楼价格添加成功</span>");
                        houtai.shop_list_ajax();
                        $("#up_building_add").fadeOut();
                    }else{
                        houtai.hint(JSON.parse(res).message);
                    }

                },
                error:function () {
                    houtai.hint("二级分类修改失败了！！");
                }
            });
        },
        set:function () {
            $.ajax({
                url: ip + "index.php?s=admincms/classify/addCatsPrice",
                type: "post",
                data: {
                    catId:houtai.linshi_catid,
                    stairsPrice:$.trim($("#louti_price_set").val()),
                    elevatorPrice:$.trim($("#dianti_price_set").val()),
                    token:get_cookie("key")
                },
                success: function (res) {
                    if (JSON.parse(res).resStatus === 0) {
                        houtai.hint("<span style='color:#0f0'>上楼价格修改成功</span>");
                        houtai.shop_list_ajax();
                        $("#up_building_set").fadeOut();
                    }else{
                        houtai.hint(JSON.parse(res).message);
                    }

                },
                error:function () {
                    houtai.hint("二级分类修改失败了！！");
                }
            });
        },
        remove:function () {
            $.ajax({
                url: ip + "index.php?s=admincms/classify/cancelCatsPrice",
                type: "post",
                data: {
                    catId:houtai.linshi_catid,
                    token:get_cookie("key")
                },
                success: function (res) {
                    if (JSON.parse(res).resStatus === 0) {
                        houtai.hint("<span style='color:#0f0'>上楼价格删除成功</span>");
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                    }else{
                        houtai.hint(JSON.parse(res).message);
                    }

                },
                error:function () {
                    houtai.hint("二级分类修改失败了！！");
                }
            });
        }
    };
    // 初始分类
    houtai.chushi();
    houtai.linshi_search.class1=$("#select1").val();
    houtai.linshi_search.class2=$("#select2").val();
    houtai.shop_list_ajax();
    // 分类一改变事件
    $("#select1").bind("change", function () {
        sessionStorage.one = $(this).val();//记录这次操作
        for (var b = 0,string2="",string3=""; b < houtai.arr.length; b++) { //循环分类查找对应的子集
            if (houtai.arr[b].pid == sessionStorage.one) {
                sessionStorage.two = houtai.arr[b].activity_list[0].cid; //记录这次操作
                for (var a = 0; a < houtai.arr[b].activity_list.length; a++) {
                    string2 += '<option value="' + houtai.arr[b].activity_list[a].cid + '">' + houtai.arr[b].activity_list[a].classnames + '</option>';
                    if(houtai.arr[b].activity_list[a].cid == sessionStorage.two){
                        for(var bb=0;bb<houtai.arr[b].activity_list[a].cats_list.length;bb++){
                            string3 += '<option value="' +houtai.arr[b].activity_list[a].cats_list[bb].catId+ '">' + houtai.arr[b].activity_list[a].cats_list[bb].catName+ '</option>';
                        }
                        $("#select3").empty().append(string3);
                    }
                }
                break;
            }
        }
        $("#select2").empty().append(string2);
        houtai.linshi_search.class1=$(this).val();
        houtai.linshi_search.class2="";
        houtai.shop_list_ajax();
    });
    // 分类二改变事件
    $("#select2").bind("change", function () {
        sessionStorage.two = $(this).val();
        for (var b = 0,string3=""; b < houtai.arr.length; b++) { //循环分类查找对应的子集
            if (houtai.arr[b].pid == sessionStorage.one) {
                for (var a = 0; a < houtai.arr[b].activity_list.length; a++) {
                    if (houtai.arr[b].activity_list[a].cid == sessionStorage.two) {
                        for (var bb = 0; bb < houtai.arr[b].activity_list[a].cats_list.length; bb++) {
                            string3 += '<option value="' + houtai.arr[b].activity_list[a].cats_list[bb].catId + '">' + houtai.arr[b].activity_list[a].cats_list[bb].catName + '</option>';
                        }
                        $("#select3").empty().append(string3);
                        break;
                    }
                }
                break;
            }
        }
        houtai.linshi_search.class1=$("#select1").val();
        houtai.linshi_search.class2=$("#select2").val();
        houtai.shop_list_ajax();
    });
    // 分类三改变事件
    $("#select3").bind("change", function () {
    });
    // 添加分类上楼按钮
    $("#add_up_btn").bind("click",function () {
        if($("#select3").val()===""||$("#select3").val()===null){
            houtai.hint("没有三级分类无法添加");
        }else{
            houtai.linshi_catid=$("#select3").val();
            $("#class3_input_add").val($("#select3 option:selected").text());
            $("#up_building_add").fadeIn();
        }
    });
    // 添加价格确认
    $(".add").bind("click",function () {
       if(houtai.jiancha()){
           houtai.add();
          houtai.chushi();
       }
    });
    // 添加价格确认
    $(".set").bind("click",function () {
       if(houtai.jiancha()){
           houtai.set();
           houtai.chushi();
       }
    });
    // 修改上楼价格
    $("#up_building_price").on("click",".set",function () {
        houtai.linshi_catid=$(this).parents("tr").find(".up_building_id").text();
        $("#class3_input_set").val($(this).parents("tr").find(".up_building_name").text());
        $("#louti_price_set").val(Number($(this).parents("tr").find(".up_building_louti").text()));
        $("#dianti_price_set").val(Number($(this).parents("tr").find(".up_building_dianti").text()));
       $("#up_building_set").fadeIn();
    });
    // 删除上楼价格
    $("#up_building_price").on("click",".remove",function () {
        houtai.linshi_catid=$(this).parents("tr").find(".up_building_id").text();
        $(".modal-body").empty().append("您确定要删除 <span style='color:#f10;'>"+$(this).parents("tr").find(".up_building_name").text()+"</span> 吗？");
        $("#modal_wrap").show();
    });
    $("#modal_close").bind("click",function () {
        $("#modal_wrap").hide();
    });
    // 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });
    $("#confirm").bind("click", function () {
        if (houtai.click_yes()) {
            houtai.remove();
            houtai.chushi();
            $("#modal_wrap").fadeOut();
        }

    });
    // 修改取消
    $(".cancle_set").bind("click", function () {
        $(".set_shangpin").fadeOut(200);
    });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});
